<?php
//$Id: base.php 194 2012-03-06 09:55:06Z tomqin@gmail.com $

/**
 * Base ARESCMS框架入口文件
 *
 * @copyright		Copyright (C) 2010-2012 ARESCMS Inc.
 * @author			TomQin <tomqin@gmail.com>
 * @license			http://www.arescms.cn/license/
 * @package Core
 */

//ARESCMS应用程序框架
if(!defined('ARESCMS_PATH')) die('Define `ARESCMS_PATH` first!');

//框架标记
define('IN_ARESCMS', true);

//路径分割符
define('DS', DIRECTORY_SEPARATOR);

//ARESCMS库框架路径
define('AC_PATH', dirname(__FILE__).DS);

//缓存文件夹地址
define('DATA_PATH', ARESCMS_PATH.'data'.DS);

//主机协议
define('SITE_PROTOCOL', isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');

//当前访问的主机名
define('SITE_URL', (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));

//来源
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');

//系统开始时间
define('SYS_START_TIME', microtime());
define('TIMESTAMP', $_SERVER['REQUEST_TIME']);

//基础函数库
require_once AC_PATH.'function'.DS.'global.func.php';

//载入核心
if(file_exists(AC_PATH.'core.min.php')){
	require_once AC_PATH.'core.min.php';
}else{
	require_once AC_PATH.'library'.DS.'db.php';
	require_once AC_PATH.'library'.DS.'view.php';
	require_once AC_PATH.'library'.DS.'config.php';
	require_once AC_PATH.'library'.DS.'cookie.php';
	require_once AC_PATH.'library'.DS.'cache.php';
	require_once AC_PATH.'library'.DS.'session.php';
}

//设置本地时差
function_exists('date_default_timezone_set') && date_default_timezone_set(Config::get('system.timezone'));

//系统字符集
define('CHARSET', Config::get('system.charset'));

//系统路径
define('STATIC_URL',  Config::get('system.static_url'));
define('UPLOAD_URL',  Config::get('system.upload_url'));
define('UPLOAD_PATH', Config::get('system.upload_path'));

//IP地址
define('IP_ADDRESS', ip_address());

//输出页面字符集
//header('Content-type: text/html; charset='.CHARSET);

//GZIP 输出模式
if(Config::get('system.gzip') && function_exists('ob_gzhandler')) {
	ob_start('ob_gzhandler');
} else {
	ob_start();
}

Class Base{

    /**
     * 载入系统方法
     *
     * @param $string	文件名
     */
	public static function loadFunction($file){
		$file = AC_PATH . 'function' . DS . strtolower($file) . '.func.php';
		if(file_exists($file)){
			require_once $file;
		}
	}

    /**
     * 载入系统类库
     *
     * @param $string	文件名
     * @return string	类名
     * @throw Exception
     */
	public static function loadLibrary($file, $init = false){
		return self::_load_class('library', $file, $init);
	}

    /**
     * 载入模型类
     *
     * @param $string	文件名
     * @return string	类名
     * @throw Exception
     */
	public static function loadModule($file, $init = false){
		Base::loadLibrary('Module');
		return self::_load_class('module', $file, $init);
	}

    /**
     * 通用载入类库
     *
     * @param string	类型
     * @param string	文件名，可以使用"/"区分目录
     * @return Object
     * @throw Exception
     */
	private static function _load_class($type, $file, $init = false){
		static $classes = array();

		$class = basename($file);
		$class = ($type == 'library') ? $class : "{$class}_{$type}";

		if(!class_exists($class)) {
			$file = AC_PATH . strtolower($type . DS . $file) . '.php';
			if(file_exists($file)){
				require_once $file;
			}else{
				new Exception('There is no '.$type.' class `' .$class. '`');
			}
		}

		if($init){
			if(isset($classes[$class]) && is_a($classes[$class], $class)){
				$object = $classes[$class];
			}else{
				$object = new $class;
				$classes[$class] = $object;
			}
			return $object;
		}
	}

	public static function adminUrl($c, $a = '', $params = array()){
		$url = '?c='.trim($c).'&a='.trim($a);
		if(!empty($params))
			$url .= '&'.http_build_query($params);

		return $url;
	}

	public static function frontUrl($c, $a = '', $other = ''){
		$url = '/'.trim($c).'/'.urlencode($a);
		if(!empty($other)){
			if(is_array($other))
				$url .= '/?'.http_build_query($other);
			else
				$url .= '/'.$other;
		}

		return $url;
	}

	public static function getCityId(){
		$cityid = Cookie::get('cityid');
		if(!$cityid){
			$cityid = Config::get('default.cityid');
		}
		return $cityid;
	}

	public static function crypt($string, $isEncrypt = true) {
		$key = Config::get('application.key');
	    $dynKey = $isEncrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40);
	    $fixedKey = hash('sha1', $key);

	    $dynKeyPart1 = substr($dynKey, 0, 20);
	    $dynKeyPart2 = substr($dynKey, 20);
	    $fixedKeyPart1 = substr($fixedKey, 0, 20);
	    $fixedKeyPart2 = substr($fixedKey, 20);
	    $key = hash('sha1', $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);

	    $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));

	    $n = 0;
	    $result = '';
	    $len = strlen($string);

	    for ($n = 0; $n < $len; $n++) {
	        $result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));
	    }
	    return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);
	}

}
?>